﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace LightningForms
{
    internal static class Helper
    {
        //REG DUDES
        public static void RegisterScripts(Control control)
        {
            control.Page.ClientScript.RegisterClientScriptResource(control.GetType(), "LightningForms.Scripts.jquery.min.js");
            control.Page.ClientScript.RegisterClientScriptResource(control.GetType(), "LightningForms.Scripts.jquery-ui.min.js");
            control.Page.ClientScript.RegisterClientScriptResource(control.GetType(), "LightningForms.Scripts.jquery-ui.timepicker.js");
            control.Page.ClientScript.RegisterClientScriptResource(control.GetType(), "LightningForms.Scripts.Localization.jquery.ui.datepicker-pt-BR.min.js");
            control.Page.ClientScript.RegisterClientScriptResource(control.GetType(), "LightningForms.Scripts.Localization.jquery.ui.timepicker-pt-BR.min.js");
            control.Page.ClientScript.RegisterClientScriptResource(control.GetType(), "LightningForms.Scripts.jquery.maskedinput.min.js");
            control.Page.ClientScript.RegisterClientScriptResource(control.GetType(), "LightningForms.Scripts.jquery.maskMoney.js");
            control.Page.ClientScript.RegisterClientScriptResource(control.GetType(), "LightningForms.Scripts.lf.js");
        }

        public static void RegisterStyles(Control control)
        {
            string url = control.Page.ClientScript.GetWebResourceUrl(control.GetType(), "LightningForms.Styles.jquery-ui.min.css");
            if (!hasStyleLink(control))
                control.Page.Header.Controls.Add(new Link(url));
        }

        //MANIPULATION
        public static void AddSiblingControl(Control reference, Control newControl)
        {
            //GETTING REFERENCE CONTAINER
            Control container = reference.Parent;
            //GETTINR REFERENCE INDEX
            int idx = container.Controls.IndexOf(reference);
            //ADD SIBLING
            container.Controls.AddAt(idx + 1, newControl);
        }

        private static bool hasStyleLink(Control control)
        {
            foreach (Control c in control.Page.Header.Controls)
                if (c.GetType() == typeof(Link)) return true;
            return false;
        }
    }

    internal static class Validators
    {
        public static RequiredFieldValidator Required(Control control, string message)
        {
            RequiredFieldValidator validator = new RequiredFieldValidator();
            validatorBasics(control, validator, message);
            return validator;
        }

        public static CompareValidator ControlCompare(Control control, ValidationDataType validationDataType, ValidationCompareOperator validationCompareOperator, string controlToCompare, string message)
        {
            CompareValidator validator = new CompareValidator();
            validatorBasics(control, validator, message);
            validator.Type = validationDataType;
            validator.ControlToCompare = controlToCompare;
            validator.Operator = validationCompareOperator;
            return validator;
        }

        public static CompareValidator ValueCompare(Control control, ValidationDataType validationDataType, ValidationCompareOperator validationCompareOperator, string valueToCompare, string message)
        {
            CompareValidator validator = new CompareValidator();
            validatorBasics(control, validator, message);
            validator.Type = validationDataType;
            validator.ValueToCompare = valueToCompare;
            validator.Operator = validationCompareOperator;
            return validator;
        }

        public static CompareValidator TypeCompare(Control control, ValidationDataType validationDataType, string message)
        {
            CompareValidator validator = new CompareValidator();
            validatorBasics(control, validator, message);
            validator.Type = validationDataType;
            validator.Operator = ValidationCompareOperator.DataTypeCheck;
            return validator;
        }

        public static RangeValidator Range(Control control, ValidationDataType validationDataType, string maximumValue, string minimumValue, string message)
        {
            RangeValidator validator = new RangeValidator();
            validatorBasics(control, validator, message);
            validator.MaximumValue = maximumValue;
            validator.MinimumValue = minimumValue;
            validator.Type = validationDataType;
            return validator;
        }

        private static void validatorBasics(Control control, BaseValidator validator, string message)
        {
            validator.ControlToValidate = control.ID;
            validator.Display = ValidatorDisplay.Dynamic;
            validator.ErrorMessage = message;
        }
    }
}